/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.imaging.jpeg.JpegSegmentReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifReader;

/**
 * @author petros
 * 
 */
public class ExifUtil {

	/**
	 * @param file
	 * @return
	 */
	private static Directory getExifDirectory(File file) throws IOException,
			JpegProcessingException {

		Metadata meta = new Metadata();
		byte[] data = new JpegSegmentReader(file)
				.readSegment(JpegSegmentReader.SEGMENT_APP1);
		new ExifReader().extract(data, meta);
		ExifIFD0Directory directory = meta
				.getDirectory(ExifIFD0Directory.class);
		return directory;

	}

	/**
	 * returns the filename based on the date information given in the exif
	 * data.
	 * 
	 * @param file
	 * @return
	 */
	public static Date getExifDate(File file) {
		Directory directory;
		try {
			directory = getExifDirectory(file);
		} catch (Exception e) {
			System.out.println("Error getting directory for file " + file.getName());
			return null;
		}
		if (directory == null) {
			System.out.println("Directory was null for " + file.getName());
			return null;
		}
		Date date = directory.getDate(ExifIFD0Directory.TAG_DATETIME);
		return date;
	}
	
	/** Returns the orientation of a jpg file.
	 * @param file
	 * @return
	 */
	public static int getExifOrientation(File file) {
		
		try {
			Directory directory = getExifDirectory(file);
			int orient = -1;
			if(directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
				orient = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
			}
			return orient;
		} catch (Exception e) {
			return -1;
		}
	}
	
}
